package com.example.its2021606134chh;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.FrameLayout;

import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBarDrawerToggle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.drawerlayout.widget.DrawerLayout;

import com.example.its2021606134chh.BusDiyModule.BusDiyFragment;
import com.example.its2021606134chh.BusModule.BusFragment;
import com.example.its2021606134chh.BusModule.BusLineFragment;
import com.example.its2021606134chh.DAModule.DataAnalysisFragment;
import com.example.its2021606134chh.DMVModule.DMVFragment;
import com.example.its2021606134chh.HighSpeedETCModule.HighSpeedETCFragment;
import com.example.its2021606134chh.LoginModule.WelcomeActivity;
import com.example.its2021606134chh.TrafficQueryModule.TraQueFragment;
import com.example.its2021606134chh.TrafficQueryModule.TrafficQueryFragment;
import com.example.its2021606134chh.TrafficViolationModule.TrafficViolationFragment;
import com.example.its2021606134chh.UserModule.UserFragment;
import com.example.its2021606134chh.fragment.ETCFragment;
import com.example.its2021606134chh.fragment.EnviFragment;
import com.example.its2021606134chh.fragment.IndexFragment;
import com.example.its2021606134chh.fragment.LifeAssistantFragment;
import com.example.its2021606134chh.LifeIndexModule.LifeIndexFragment;
import com.example.its2021606134chh.TLModule.LightFragment;
import com.example.its2021606134chh.fragment.MyCarFragment;
import com.example.its2021606134chh.fragment.MyMessageFragment;
import com.example.its2021606134chh.fragment.RealTimeEnvironmentFragment;
import com.example.its2021606134chh.fragment.TMragment;
import com.google.android.material.navigation.NavigationView;

public class MainActivity extends AppCompatActivity {
    private Toolbar toolbar;
    private DrawerLayout drawerLayout;
    private FrameLayout frameLayout;
    private NavigationView Navigation;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        setSupportActionBar(toolbar);   //使用Toolbar替换系统自带的ActionBar
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);  //左上角显示返回键
        getSupportActionBar().setHomeButtonEnabled(true);   //设置返回键可用
//        将工具栏和侧滑菜单绑定
//        创建切换对象toggle
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar, R.string.app_name, R.string.app_name) {
            @Override
            public void onDrawerOpened(View drawerView) {
                super.onDrawerOpened(drawerView);
            }
            @Override
            public void onDrawerClosed(View drawerView) {
                super.onDrawerClosed(drawerView);
            }
        };
        toggle.syncState(); //设置为同步
        drawerLayout.addDrawerListener(toggle);

        int dmv = getIntent().getIntExtra("DMV", 0);
        boolean recharge = getIntent().getBooleanExtra("Recharge", false);
        if (dmv == 1) {
            getSupportFragmentManager().beginTransaction()
                    .add(R.id.frameLayout, IndexFragment.newInstance(recharge, "username", ""), "")
                    .commit();
        } else {
            //        初始化Fragment
            getSupportFragmentManager().beginTransaction()
                    .add(R.id.frameLayout, IndexFragment.newInstance("username", ""), "")
                    .commit();
        }
        toolbar.setTitle("个人中心");


//        给侧滑菜单设置选择事件，在处理单元中，根据选项显示Fragment
        Navigation.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
            @SuppressLint("NonConstantResourceId")
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                item.setCheckable(true);    //当前选项
                switch (item.getItemId()) {
                    case R.id.ItemIndex:
                        getSupportFragmentManager().beginTransaction()
                                .replace(R.id.frameLayout, IndexFragment.newInstance("username", ""), "")
                                .commit();
                        toolbar.setTitle("个人中心");
                        break;
                    case R.id.ItemETC:
                        getSupportFragmentManager().beginTransaction()
                                .replace(R.id.frameLayout, ETCFragment.newInstance("", ""), "")
                                .commit();
                        toolbar.setTitle("ETC账户");
                        break;
                    case R.id.ItemMyCar:
                        getSupportFragmentManager().beginTransaction()
                                .replace(R.id.frameLayout, MyCarFragment.newInstance("", ""), "")
                                .commit();
                        toolbar.setTitle("我的座驾");
                        break;
                    case R.id.ItemMyMessage:
                        getSupportFragmentManager().beginTransaction()
                                .replace(R.id.frameLayout, MyMessageFragment.newInstance("", ""), "")
                                .commit();
                        toolbar.setTitle("我的消息");
                        break;
                    case R.id.ItemDMV:
                        getSupportFragmentManager().beginTransaction()
                                .replace(R.id.frameLayout, DMVFragment.newInstance("", ""), "")
                                .commit();
                        toolbar.setTitle("车管局");
                        break;
                    case R.id.ItemHSETC:
                        getSupportFragmentManager().beginTransaction()
                                .replace(R.id.frameLayout, HighSpeedETCFragment.newInstance("", ""), "")
                                .commit();
                        toolbar.setTitle("高速ETC");
                        break;
                    case R.id.ItemTV:
                        getSupportFragmentManager().beginTransaction()
                                .replace(R.id.frameLayout, TrafficViolationFragment.newInstance("", ""), "")
                                .commit();
                        toolbar.setTitle("车辆违章");
                        break;
                    case R.id.ItemDA:
                        getSupportFragmentManager().beginTransaction()
                                .replace(R.id.frameLayout, DataAnalysisFragment.newInstance("", ""), "")
                                .commit();
                        toolbar.setTitle("数据分析");
                        break;
                    case R.id.ItemTM:
                        getSupportFragmentManager().beginTransaction()
                                .replace(R.id.frameLayout, TMragment.newInstance("", ""), "")
                                .commit();
                        toolbar.setTitle("出行管理");
                        break;
                    case R.id.ItemEnvi:
                        getSupportFragmentManager().beginTransaction()
                                .replace(R.id.frameLayout, EnviFragment.newInstance("", ""), "")
                                .commit();
                        toolbar.setTitle("环境指标");
                        break;
                    case R.id.ItemLiveAssistant:
                        getSupportFragmentManager().beginTransaction()
                                .replace(R.id.frameLayout, LifeAssistantFragment.newInstance("", ""), "")
                                .commit();
                        toolbar.setTitle("生活助手");
                        break;
                    case R.id.ItemLifeIndex:
                        getSupportFragmentManager().beginTransaction()
                                .replace(R.id.frameLayout, LifeIndexFragment.newInstance("", ""), "")
                                .commit();
                        toolbar.setTitle("生活指数");
                        break;
                    case R.id.ItemRealTimeEnvironment:
                        getSupportFragmentManager().beginTransaction()
                                .replace(R.id.frameLayout, RealTimeEnvironmentFragment.newInstance("", ""), "")
                                .commit();
                        toolbar.setTitle("实时环境");
                        break;
                    case R.id.ItemLight:
                        getSupportFragmentManager().beginTransaction()
                                .replace(R.id.frameLayout, LightFragment.newInstance("", ""), "")
                                .commit();
                        toolbar.setTitle("交通灯");
                        break;
                    case R.id.ItemBus:
                        getSupportFragmentManager().beginTransaction()
                                .replace(R.id.frameLayout, BusFragment.newInstance("", ""), "")
                                .commit();
                        toolbar.setTitle("公交查询");
                        break;
                    case R.id.ItemBusLine:
                        getSupportFragmentManager().beginTransaction()
                                .replace(R.id.frameLayout, BusLineFragment.newInstance("", ""), "")
                                .commit();
                        toolbar.setTitle("实时交通");
                        break;
                    case R.id.ItemBusDiy:
                        getSupportFragmentManager().beginTransaction()
                                .replace(R.id.frameLayout, BusDiyFragment.newInstance("", ""), "")
                                .commit();
                        toolbar.setTitle("定制班车");
                        break;
                    case R.id.ItemTrafficQuery:
                        getSupportFragmentManager().beginTransaction()
                                .replace(R.id.frameLayout, TrafficQueryFragment.newInstance("", ""), "")
                                .commit();
                        toolbar.setTitle("路况查询");
                        break;
                    case R.id.ItemUser:
                        getSupportFragmentManager().beginTransaction()
                                .replace(R.id.frameLayout, UserFragment.newInstance("", ""), "")
                                .commit();
                        toolbar.setTitle("用户管理");
                        break;
                    case R.id.ItemExit:
                        Intent intent = new Intent(MainActivity.this, WelcomeActivity.class);
                        startActivity(intent);
                        finish();
                        break;

                }
                return true;
            }
        });
    }

    private void initView() {
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        drawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout);
        frameLayout = (FrameLayout) findViewById(R.id.frameLayout);
        Navigation = (NavigationView) findViewById(R.id.Navigation);
    }

}